package com.siwei.mes.job;

import com.siwei.mes.common.util.DateHelper;
import com.siwei.mes.entity.system.Serial;
import com.siwei.mes.enums.GenerateCodeEnum;
import com.siwei.mes.enums.IsCleanEnum;
import com.siwei.mes.enums.RoleEnum;
import com.siwei.mes.mapper.system.SerialMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 定时清空
 *
 * @Description: 定时清空
 * @Version: 1.0
 * @author: chenmaolin
 * @date: 2024/1/28 21:47
 */
@Component
public class CleanSerialJob {
    private static final Logger log = LoggerFactory.getLogger("job");
    @Resource
    private SerialMapper serialMapper;

    /**
     * 自动清空表数据 每天0点清空一次
     */
    @Scheduled(cron = "0 0 0 * * ? ")
    public void init() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        String year = DateHelper.getYear() < 10 ? "0" + DateHelper.getYear() : DateHelper.getYear().toString();
        String month = DateHelper.getMonth() < 10 ? "0" + DateHelper.getMonth() : DateHelper.getMonth().toString();
        String day = DateHelper.getDay() < 10 ? "0" + DateHelper.getDay() : DateHelper.getDay().toString();
        log.info("定时清空序号表开始");
        /**查询所有的数据*/
        List<Serial> serialList = serialMapper.getList(new Serial().setCleanType(1));
        for (Serial serial : serialList) {
            /**判断是否需要重置**/
            if (serial.getIsClean().equals(IsCleanEnum.NO.getCode())) {
                continue;
            }
            serial.setSerialYear(year);
            serial.setSerialMonth(month);
            serial.setSerialDay(day);
            serial.setSerialNumber(year.substring(2, 4) + month + day + "000");
            /**如果是混凝土这里需要重新设置一下*/
            if (serial.getSerialNo().equals(GenerateCodeEnum.EXPERIMENT.getCode())) {
                serial.setSerialNumber(year.substring(2, 4) + month + day + "00000");
            }
            serialMapper.updateByPrimaryKeySelective(serial);
        }
        stopWatch.stop();
        log.info("定时清空序号表完成,耗时：{}s", stopWatch.getTotalTimeMillis() / 1000);
    }

    /**
     * 自动清空表数据 每年0点清空一次
     */
    @Scheduled(cron = "0 0 0 1 1 ? ")
    public void initYear() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        log.info("定时清空序号表开始");
        String year = DateHelper.getYear() < 10 ? "0" + DateHelper.getYear() : DateHelper.getYear().toString();
        String month = DateHelper.getMonth() < 10 ? "0" + DateHelper.getMonth() : DateHelper.getMonth().toString();
        String day = DateHelper.getDay() < 10 ? "0" + DateHelper.getDay() : DateHelper.getDay().toString();
        /**查询所有的数据*/
        List<Serial> serialList = serialMapper.getList(new Serial().setCleanType(2));
        for (Serial serial : serialList) {
            /**判断是否需要重置**/
            if (serial.getIsClean().equals(IsCleanEnum.NO.getCode())) {
                continue;
            }
            serial.setSerialYear(year);
            serial.setSerialMonth(month);
            serial.setSerialDay(day);
            serial.setSerialNumber(year.substring(2, 4) + month + day + "000");
            List<String> serialNoList = new ArrayList<>();
            /**如果是企业设置中的编号配置这里需要重新设置一下*/
            serialNoList.add(GenerateCodeEnum.QUALITY_CERTIFICATE.getCode());
            serialNoList.add(GenerateCodeEnum.OPEN_APPRAISAL_LEDGER.getCode());
            serialNoList.add(GenerateCodeEnum.MIX_PROPORTION.getCode());
            if (serialNoList.contains(serial.getSerialNo())) {
                serial.setSerialNumber("00000");
            }
            serialMapper.updateByPrimaryKeySelective(serial);
        }
        stopWatch.stop();
        log.info("定时清空序号表完成,耗时：{}s", stopWatch.getTotalTimeMillis() / 1000);
    }
}
